const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  data: {
    currentIndex: 0, //当前索引
    type: '0',  //工程类型
    project_id: null, //项目id
    icon: {
      normal: '../../images/report/unselect_x.png',
      active: '../../images/report/select_3x.png',
    },
    //请求到的详情数据
    detail:null,
    //项目进度
    process_list: [],
    showspeed: {
      normal: '../../images/report/unselect_2x.png',
      active: '../../images/report/select_3x.png',
    },
    delShow: null,
    //项目的代号
    fileSort: 0,
    filePath: []  //存储远程文件路径
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    
    this.getDetail(options)
    this.getprojectId(options)
    //判断是否显示删除
    if(options.orderNumber == '驳回') {
      this.setData({
        delShow: true
      })
    }else {
      this.setData({
        delShow: false
      })
    }
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //请求详情数据
  getDetail(options) {
    const project_id = options.id;
    util.request(api.project_detail, {project_id}, 'get').then(res => {
      // console.log(res);
      this.setData({
        type: res.data.detail.type,
        detail: res.data.detail,
        project_id
      })
      let detail = res.data.detail
      if (detail.fileVoList) {
        let that = this;
        for (let i = 0; i < detail.fileVoList.length; i++) {
          let filePathObj = {}
            filePathObj.path = ""
            that.data.filePath.push(filePathObj)
            that.setData({
              filePath: that.data.filePath
            })
        }
      }
    })
  },
  //项目进度
  getprojectId(options) {
    const project_id = options.id;
    util.request(api.project_schedule, {project_id}, 'get').then(res => {
      // console.log(res);
      this.setData({
        process_list: res.data.process_list
      })
    })
  },

  //改变索引
  currentIndexBtn(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  //删除项目
  delProject() {
    const project_id = this.data.project_id;
    wx.showModal({
      title: '提示',
      content: '确定删除该项目吗',
      success (res) {
        if (res.confirm) {
          util.request(api.project_delete, {project_id}, 'GET' ).then(res => {
            console.log(res);
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1000
            }).then(res => {
              wx.reLaunch({
                url: '/pages/list/list'
              })
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  //打开文件
  openDoc(e) {
    let {detail} = this.data;
    let i = e.currentTarget.dataset.index;
    const fileId = detail.fileVoList[i].id;
    const fileSort = this.data.fileSort;
    const that = this;
   
    util.request(api.downloadFile, {fileId, fileSort}, 'GET').then(res => {
      // console.log(res.data.path);
      let path = res.data.path
      //下载文件 将路径转换为本地临时路径可以打开
      // var types = that.getFileType(path)
      // console.log(types);
      wx.downloadFile({
        url: path,
        // filePath: wx.env.USER_DATA_PATH + "/invoice" + "." + types,  //自定义文件地址
        header: {
          'Content-Type': 'application/json',
          'Authorization': wx.getStorageSync('token')
        },
        success(res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调
          if (res.statusCode === 200) {
            console.log(res);
            that.setData({
              [`filePath[${i}].path`]: res.tempFilePath
            })
            let filePath = that.data.filePath[i].path;
            //打开文件
            wx.openDocument({
              showMenu: true,
              filePath,
              success: function (res) {
                // console.log(res);
              },
              fail(err) {
                console.log(err);
              }
            })
          }

        }
      })
    })

   
  },
  //生成路径
  getFileType(filePath) {
    var startIndex = filePath.lastIndexOf(".");
    if(startIndex != -1) {
      return filePath.substring(startIndex + 1, filePath.length).toLowerCase();
    }else {
      return "";
    }
  },
  updateProject() {
    const detail = JSON.stringify(this.data.detail);
    wx.navigateTo({
      url: '/pages/updata/updata?detail=' + detail
    })
  }
  
})